from django.db.models import *
from django.utils.safestring import mark_safe
from pyplog.artists.models import Artist
from pyplog.blog.archive import archive
from pyplog.blog.utils import convert_markup, markup_choices, PublishedManager

class Category(Model):
	title = CharField(default = "New category", max_length = 64)
	slug = SlugField(unique = True)
	
	class Meta:
		verbose_name = "category"
		verbose_name_plural = "categories"
		
	def __unicode__(self):
		return self.title
	
	@permalink
	def get_absolute_url(self):
		return ("category-posts", (), { "slug" : self.slug, "page" : "" })
	
	@permalink
	def get_feed_url(self):
		return ("post-feed", (), { "url" : "category/" + self.slug })
	
	@property
	def postset_description(self):
		return mark_safe("Blog posts in the <i>" + self.title + "</i> category.")

class Post(Model):
	title = CharField(default = "New post", max_length = 64)
	slug = SlugField(unique = True)
	author = ForeignKey(Artist, blank = True, null = True, related_name = "posts")
	published = DateTimeField("Publishing date", help_text = "If you set a date in the future, the post will not be displayed publicly until that date.")
	categories = ManyToManyField(Category, blank = True, null = True, related_name = "posts")
	content = TextField()
	content_markup = CharField("Markup", max_length = 3, default = "HTM", choices = markup_choices)
	content_html = TextField()
	
	public_objects = PublishedManager()
	all_objects = Manager()
	
	class Meta:
		ordering = ["-published"]
		
	def __unicode__(self):
		return self.title
	
	@permalink
	def get_absolute_url(self):
		return ("post-detail", (), { "slug" : self.slug })
	
	def save(self):
		self.content_html = convert_markup(self.content, self.content_markup)
		super(Post, self).save()

signals.post_save.connect(archive.update, sender = Post)
signals.post_delete.connect(archive.update, sender = Post)
archive.update(Post)
